<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class AdminPermission extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'display_name',
        'description',
        'module',
        'type'
    ];

    // 关联角色权限
    public function rolePermissions()
    {
        return $this->hasMany(AdminRolePermission::class, 'permission_id');
    }

    // 权限类型常量
    const TYPE_MENU = 'menu';               // 菜单权限
    const TYPE_BUTTON = 'button';           // 按钮权限
    const TYPE_API = 'api';                 // API权限
    const TYPE_DATA = 'data';               // 数据权限

    // 获取权限类型显示名称
    public function getTypeDisplayAttribute()
    {
        $types = [
            self::TYPE_MENU => '菜单权限',
            self::TYPE_BUTTON => '按钮权限',
            self::TYPE_API => 'API权限',
            self::TYPE_DATA => '数据权限'
        ];

        return $types[$this->type] ?? $this->type;
    }
}